package net.luoran.dvm.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.luoran.dvm.common.base.PageFactory;
import net.luoran.dvm.common.base.Result;
import net.luoran.dvm.common.util.JdbcUtil;
import net.luoran.dvm.entity.vo.PanelModuleContentVO;
import net.luoran.dvm.entity.PanelModuleDO;
import net.luoran.dvm.entity.dto.PanelModuleDTO;
import net.luoran.dvm.service.ElementService;
import net.luoran.dvm.service.PanelModuleService;
import net.luoran.dvm.service.PanelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: miao wenbin
 * @since: 2020/11/25 10:40
 * @description:
 * @version: 1.0
 */
@Controller
@RequestMapping("/module")
public class PanelModuleController {

    @Value("${server.servlet.context-path}")
    public String contextPath;

    @GetMapping
    public String panelMain(Model model) {
        model.addAttribute("contextPath", contextPath);
        return "module";
    }

    @Autowired
    private PanelModuleService panelModuleService;

    @PostMapping("/queryList")
    @ResponseBody
    public Result queryList(@RequestBody PanelModuleDTO panelModuleDTO) {
        Result result = Result.ok();
        try {
            PanelModuleContentVO panelModuleContent = new PanelModuleContentVO();
            if (panelModuleDTO.getPanelName() != null) {
                panelModuleContent.setPanelName(panelModuleDTO.getPanelName());
            } else {
                panelModuleContent.setPanelName("");
            }
            Page<PanelModuleContentVO> results = panelModuleService.selectPanelModuleByName(PageFactory.createPageInfo(panelModuleDTO.getCurrent(), panelModuleDTO.getSize()), panelModuleContent);
            result.setData(results);
            result.setDraw(panelModuleDTO.getDraw() + 1);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("系统错误");
        }
        return result;
    }

    @PostMapping("/insertModule")
    @ResponseBody
    public Result insertModule(@RequestBody PanelModuleDO panelModuleDO) {
        Result result = Result.ok();
        try {
            if (panelModuleDO != null) {
                if (panelModuleDO.getModuleId() != null) { //修改
                    //先将旧的查出来
                    PanelModuleDO oldModule = panelModuleService.getById(panelModuleDO.getModuleId());
                    if (oldModule != null) {
                        oldModule.setDataSource(panelModuleDO.getDataSource());
                        panelModuleService.updateById(oldModule);
                    }
                }
            } else {
                return Result.error("传入参数为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("系统错误");
        }
        return result;
    }

    /**
     * 前台 查询数据源
     *
     * @param database
     */
    @PostMapping("/getModuleDatabase")
    @ResponseBody
    public Result queryDataSource(String database) {
        Result result = Result.ok();
        try {
            if (database != null) {
                Map<String, String> resultMap = new HashMap<String, String>();
                String[] data = database.split("\\|");

                if (data.length < 4) {
                    return Result.error("请检查格式是否正确");
                }

                for (int i = 0; i < data.length; i++) {
                    if (i == 0) {
                        String url = data[i].toString();
                        resultMap.put("url", url);
                    }
                    if (i == 1) {
                        String userName = data[i].toString();
                        resultMap.put("user", userName);
                    }
                    if (i == 2) {
                        String password = data[i].toString();
                        resultMap.put("password", password);
                    }
                    if (i == 3) {
                        int index = StrUtil.ordinalIndexOf(database, "|", 3);
                        String sql = database.substring(index + 1);
                        resultMap.put("sql", sql);
                    }
                }
                List<Map<String, String>> allData = JdbcUtil.queryDataSource(resultMap);
                JSONArray array = JSONUtil.parseArray(allData);
                result.setData(array);
            } else {
                return Result.error("前台传入database为空！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
